Workshop: ggplot2

Tag 1

Author

Arieja Farugie & Luca Schnatz

Published

April 10, 2023

Datensatz laden

Zunächst laden wir den Datensatz:

library(tidyverse)
library(here)

exercise_data <- read_rds(here("data/exercise_data/clean_exercise_data.rds"))
glimpse(exercise_data)
Rows: 3,471
Columns: 32
$ id_participant                       <dbl> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11…
$ geschlecht                           <fct> weiblich, weiblich, maennlich, ma…
$ alter                                <dbl> 33, 50, 56, 61, 59, 56, 66, 25, 5…
$ familienstand                        <fct> ledig, verheirat.zusam.leb., verh…
$ nettoeinkommen_frei                  <dbl> 1800, 1100, 2500, NA, 1000, NA, 1…
$ nettoeinkommen_binned                <fct> 1750 - 1999 euro, 1000 - 1124 eur…
$ aequivalenzeinkommen                 <dbl> 1800, 2467, 1750, 4167, 1000, 466…
$ konfession                           <fct> evang.ohne freikirch, keiner reli…
$ erwerb_1                             <fct> erwerbstaetig, erwerbstaetig, erw…
$ erwerb_2                             <fct> "erwerbstaetig", "erwerbstaetig",…
$ schulabschluss                       <fct> "hochschulreife", "mittlere reife…
$ berufstaetig                         <fct> hauptberufl.ganztags, hauptberufl…
$ berufliche_stellung                  <fct> "angestellter", "angestellter", "…
$ staatsbuergerschaft                  <fct> deutschland, deutschland, deutsch…
$ deutsch_staats                       <fct> "ja", "ja", "ja", "ja", "ja", "ja…
$ bundesland                           <fct> mecklenb.-vorpommern, thueringen,…
$ ost_west                             <fct> neue bundeslaender, neue bundesla…
$ subj_schichteinstufung               <fct> mittelschicht, mittelschicht, unt…
$ bmi                                  <dbl> 23.53, 28.23, 24.77, 38.87, 30.82…
$ ps_01_gehetzt                        <int> 4, 4, 3, 2, 1, 2, 4, 2, 1, 3, 3, …
$ ps_02_niedergeschlagen               <int> 1, 4, 2, 3, 1, 2, 2, 3, 1, 1, 1, …
$ ps_03_ausgeglichen                   <int> 2, 4, 2, 2, 1, 4, 4, 3, 2, 2, 1, …
$ ps_04_energetisch                    <int> 2, 4, 2, 3, 1, 5, 2, 2, 3, 3, 3, …
$ ps_05_schmerzen                      <int> 1, 3, 1, 4, 1, 1, 4, 1, 4, 3, 1, …
$ ps_06_einsam                         <int> 1, 2, 1, 2, 1, 1, 1, 1, 3, 2, 1, …
$ ps_07_gesundeinschr_koerperlich_allg <int> 1, 3, 2, 3, 1, 1, 3, 3, 1, 1, 1, …
$ ps_08_gesundeinschr_koerperlich_art  <int> 1, 3, 2, 3, 1, 1, 4, 3, 3, 1, 1, …
$ ps_09_gesundeinschr_seelisch_allg    <int> 1, 2, 2, 2, 1, 1, 3, 1, 1, 1, 1, …
$ ps_10_gesundeinschr_seelisch_art     <int> 1, 1, 2, 2, 1, 1, 4, 1, 1, 1, 1, …
$ ps_11_einschr_sozial                 <int> 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1, …
$ lebenszufriedenheit                  <dbl> 8, 8, 5, 8, 8, 8, 8, 8, 10, 8, 8,…
$ psychological_distress               <int> 16, 31, 20, 29, 11, 20, 32, 21, 2…

Basics von ggplot2

  • wir werden die auf ganz einfacher Ebene die wichtigsten Arten von Plots kennenlernen
  • anhand der verschiedenen Arten von Daten (numerisch, kategorisch)

1 Variable (Numerisch)

Boxplot

  • kurze Beschreibung eines Boxplots?
  • wichtig, dass bei einer Variablen, die andere Achse keine Bedeutung besitzt
ggplot(
  data = exercise_data,
  aes(y = psychological_distress)
  ) +
  geom_boxplot()
Warning: Removed 25 rows containing non-finite values (`stat_boxplot()`).

ggplot(
  data = exercise_data,
  aes(x = psychological_distress)
  ) +
  geom_boxplot()
Warning: Removed 25 rows containing non-finite values (`stat_boxplot()`).

Histogramm

  • keep in mind: bins matter
ggplot(
  data = exercise_data,
  aes(x = psychological_distress)
  ) +
  geom_histogram()
`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
Warning: Removed 25 rows containing non-finite values (`stat_bin()`).

ggplot(
  data = exercise_data,
  aes(x = psychological_distress)
  ) +
  geom_histogram(bins = 10)
Warning: Removed 25 rows containing non-finite values (`stat_bin()`).

ggplot(
  data = exercise_data,
  aes(x = psychological_distress)
  ) +
  geom_histogram(bins = 40)
Warning: Removed 25 rows containing non-finite values (`stat_bin()`).

Density-Plots

ggplot(
  data = exercise_data,
  aes(x = psychological_distress)
  ) +
  geom_density()
Warning: Removed 25 rows containing non-finite values (`stat_density()`).

1 Variable (Kategorisch)

Barplot

  • Wann ist Barplot sinnvoll?
  • Was stellt er in dem Beispiel dar?
ggplot(
  data = exercise_data,
  mapping = aes(x = subj_schichteinstufung)
  ) + 
  geom_bar(stat = "count")

ggplot(
  data = exercise_data,
  mapping = aes(y = subj_schichteinstufung)
  ) + 
  geom_bar(stat = "count")

ggplot(
  data = exercise_data,
  mapping = aes(x = subj_schichteinstufung,
                y = after_stat(count/sum(count)))
  ) + 
  geom_bar()

2 Variablen (Numerisch, Numerisch)

ggplot(
  data = exercise_data,
  mapping = aes(x = alter,
                y = bmi)
  ) +
  geom_point()  
Warning: Removed 55 rows containing missing values (`geom_point()`).

ggplot(
  data = exercise_data,
  mapping = aes(x = alter,
                y = bmi,
                color = geschlecht)
) + 
  geom_point()
Warning: Removed 55 rows containing missing values (`geom_point()`).

ggplot(
  data = exercise_data,
  mapping = aes(x = alter,
                y = bmi)
                
  ) + 
  geom_point() + 
  facet_grid(cols = vars(geschlecht)) 
Warning: Removed 55 rows containing missing values (`geom_point()`).

ggplot(
  data = exercise_data,
  mapping = aes(x = alter,
                y = bmi,
                color = geschlecht)
) + 
  geom_point() + 
  facet_grid(cols = vars(geschlecht)) 
Warning: Removed 55 rows containing missing values (`geom_point()`).

2 Variablen (Kategorisch, Kategorisch)

ggplot(
  data = exercise_data,
  mapping = aes(x = subj_schichteinstufung, fill = geschlecht)
  ) + 
  geom_bar(stat = "count", position = position_dodge(width = 1))

ggplot(
  data = exercise_data,
  mapping = aes(x = subj_schichteinstufung, fill = geschlecht)
  ) + 
  geom_bar(stat = "count", position = position_stack())

2 Variablen (Numerisch, Kategorisch)

ggplot(
  data = exercise_data,
  mapping = aes(x = subj_schichteinstufung,
                y = psychological_distress)
  ) + 
  geom_boxplot()
Warning: Removed 25 rows containing non-finite values (`stat_boxplot()`).

Mehr als zwei Variablen

ggplot(
  data = exercise_data,
  mapping = aes(x = alter,
                y = bmi,
                color = geschlecht)
) + 
  geom_point(alpha = 0.7) + 
  facet_grid(cols = vars(geschlecht)) 
Warning: Removed 55 rows containing missing values (`geom_point()`).

  • Welche Basics Plots sollten besprochen werden?
  • Barplot, Scatter, Boxplot, Density, Histogramm, Pie, Donought
  • Idee: Vorstellung der Plots aufteilen nach Art der Daten
    • Numerisch (1 Variable), Numerisch (2 Variablen)
    • Kategorisch (1 Variable)